<template>
  <div class="login-container">
    <el-form ref="formRef" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">尚品汇后台管理</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon name="ele-UserFilled" />
        </span>
        <el-input ref="username" v-model="loginForm.username" placeholder="请输入账号"  />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon name="ele-Lock" />
        </span>
        <el-input :key="passwordType" ref="passwordRef" v-model="loginForm.password" :type="passwordType" placeholder="请输入密码"  @keyup.enter.native="handleLogin" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :name="passwordType === 'password' ? 'ele-Hide' : 'ele-View'" />
        </span>
      </el-form-item>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;height: 40px;" @click.prevent="handleLogin">登 陆</el-button>
    </el-form>
  </div>
</template>

<script lang="ts">
export default {
  name: 'Login'
}
</script>

<script lang="ts" setup>
import { useUserInfoStore } from '@/stores/userInfo'
import type { FormInstance } from 'element-plus'
import { nextTick, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
// 获取用户相关的小仓库
const userInfoStore = useUserInfoStore()
// 获取路由
const route = useRoute()
// 获取路由器
const router = useRouter()
//表单收集的用户信息:账号与密码
//当前账号admin|111111超级管理员账号
const loginForm = ref({
  username: 'admin',
  password: '111111'
})
//登录按钮加载小圈的布尔值设置
const loading = ref(false)
// 密码表单元素类型:默认密码类型
const passwordType = ref('password')
// 获取路径上的query参数
const redirect = ref('')
// 获取表单密码元素的组件实例
const passwordRef = ref<HTMLInputElement>()
//获取el-form组件实例
const formRef = ref<FormInstance>()

// 用户名字自定义校验规则
const validateUsername = (rule: any, value: any, callback: any) => {
  if (value.length < 4) {
    // 账号少于4位不通过
    callback(new Error('用户名长度不能小于4位'))
  } else {
    // 大于4位就校验账号成功
    callback()
  }
}
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length < 6) {
    // 密码少于6位不通过
    callback(new Error('密码长度不能小于6位'))
  } else {
    // 大于6位就校验密码成功
    callback()
  }
}

// 表单校验规则对象
const loginRules = {
  username: [{ required: true, validator: validateUsername }],
  password: [{ required: true, trigger: 'blur', validator: validatePassword }]
}

// 监听路由的变化:立即监听;路由发生变化重定向到layout的二级路由组件 /home
watch(
  route,
  () => {
    redirect.value = route.query && (route.query.redirect as string)
  },
  { immediate: true }
)

/* 
切换密码的显示/隐藏
*/
const showPwd = () => {
  if (passwordType.value === 'password') {
    // 如果密码为password,点击切换text
    passwordType.value = 'text'
  } else {
    // 如果密码为text,点击切换password
    passwordType.value = 'password'
  }
  nextTick(() => {
    passwordRef.value?.focus() 
  })
}

/* 
点击登陆的回调 || 输入完密码enter键也可以触发登录回调
*/
const handleLogin = async () => {
  // 校验表单数据,最终返回一个promise,用await等待成功的结果
  await formRef.value?.validate()
  // 控制登录按钮的加载效果
  loading.value = true
  //整理登录的参数:账号与密码
  const { username, password } = loginForm.value
  try {
    // 发请求登录成功,会拿到token
    await userInfoStore.login(username, password)
    // 如果登录成功:进入首页或者是之间去想去没有去到的地方
    router.push({ path: redirect.value || '/' })
  } finally {
    // 最终promise.finally 不管登录成功还是失败都加载效果消息
    loading.value = false
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  background-image: url(../../assets/bg.jpg);
  background-color: #2d3a4b;
  background-size: cover;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    .el-input__wrapper {
      width: 100%;
      background-color: transparent;
      box-shadow: none;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 10px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
